﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NRCP.BusinessLogicLayer;
using NRCP.Helpers;
using System.Globalization;
using NRCP.BusinessEntities;
using NRCP.App_Service;

namespace NRCP
{
    public partial class Monthly_Zonal_Plan : NationalPlanBase
    {
        public Int32 DivisionId
        {
            get
            {
                return Convert.ToInt32(ViewState["DivisionId"]);
            }
            set
            {
                ViewState["DivisionId"] = value;
            }
        }

        public Int32 FishId
        {
            get
            {
                return Convert.ToInt32(ViewState["FishId"]);
            }
            set
            {
                ViewState["FishId"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PreLoadData();

            }
        }

        private void PreLoadData()
        {
            Int32 fishId = Convert.ToInt32(SiteUtility.GetQueryParameter("fishId"));
            Int32 divisionId = Convert.ToInt32(SiteUtility.GetQueryParameter("divisionId"));
            DivisionId = divisionId;

            NationalPlanFishBLL bll = new NationalPlanFishBLL();
            List<NationalPlanFish> nationalPlanFishes = bll.GetNationalPlanFishes(obj => obj.NationalPlanId == SiteUtility.IntQueryParam && obj.FishId == fishId);
            if (nationalPlanFishes.Count > 0)
            {
                FishId = nationalPlanFishes[0].Id;
                litGroupName.Text = nationalPlanFishes[0].Fish.Name;
            }
            var months = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames.TakeWhile(m => m != String.Empty).Select((m, i) => new { Month = i + 1, MonthName = m }).ToList();

            ddlMonth.DataSource = months;
            ddlMonth.DataTextField = "MonthName";
            ddlMonth.DataValueField = "Month";
            ddlMonth.DataBind();


            NationalPlanDetailsYearlyBLL nationalPlanDetailsYearlyBLL = new NationalPlanDetailsYearlyBLL();
            List<NationalPlanDetailsYearly> nationalPlanDetailsYearlys = nationalPlanDetailsYearlyBLL.GetNationalPlanDetailsYearlys(obj => obj.NationalPlanId == SiteUtility.IntQueryParam && obj.FishId == FishId && obj.DivisionId == divisionId);
            ddlChemicalGroup.DataSource = nationalPlanDetailsYearlys.ToList().ConvertAll(x => new TempValueObject { Key = x.NationalPlanChemicalGroup.ChemicalGroup.Name, Value = x.ChemicalGroupId.ToString() });
            ddlChemicalGroup.DataTextField = "Key";
            ddlChemicalGroup.DataValueField = "Value";
            ddlChemicalGroup.DataBind();

            SiteDataUtility utility = new SiteDataUtility();
            ddlDistrics.DataSource = utility.LoadDistrictByPlanDivision(SiteUtility.IntQueryParam, divisionId);
            ddlDistrics.DataTextField = "Key";
            ddlDistrics.DataValueField = "Value";
            ddlDistrics.DataBind();

            List<TempValueObject> plansummary = utility.LoadPlanSummaryByDivisionAndFish(SiteUtility.IntQueryParam, divisionId, fishId);
            SessionManager.TEMP_PLAN_SUMMARY = plansummary;


            SubDistrictBLL upazillaBll = new SubDistrictBLL();
            List<SubDistrict> upazillas = upazillaBll.GetAllSubDistricts();
            SessionManager.UPAZILA = upazillas;

            ddlUpazila.DataSource = upazillas.Where(obj => obj.DistrictId == Convert.ToInt32(ddlDistrics.SelectedItem.Value)).ToList();
            ddlUpazila.DataTextField = "Name";
            ddlUpazila.DataValueField = "Id";
            ddlUpazila.DataBind();
            //upnlUpazila.Update();


            List<NationalPlanDetailsMonthly> monthlylist = SessionManager.NATIONALPLAN_MONTHLY;
            monthlylist.Clear();
            SessionManager.NATIONALPLAN_MONTHLY = monthlylist;

            List<TempMonthScheduler> tmpList = SessionManager.TEMP_NATIONALPLAN_MONTHLY;
            tmpList.Clear();
            SessionManager.TEMP_NATIONALPLAN_MONTHLY = tmpList;

            List<TempValueObject> tmpVal = SessionManager.TEMP_VAL_CONTAINER;
            tmpVal.Clear();
            SessionManager.TEMP_VAL_CONTAINER = tmpVal;

        }

        protected void ddlDistrics_SelectedIndexChanged(object sender, EventArgs e)
        {
            Int32 districtId = Convert.ToInt32(ddlDistrics.SelectedItem.Value);
            List<SubDistrict> upazillas = SessionManager.UPAZILA;

            if (upazillas.Count == 0)
            {
                SubDistrictBLL upazillaBll = new SubDistrictBLL();
                upazillas = upazillaBll.GetAllSubDistricts();
                SessionManager.UPAZILA = upazillas;
            }

            if (upazillas != null)
            {
                ddlUpazila.ClearSelection();
                ddlUpazila.DataSource = upazillas.Where(obj => obj.DistrictId == districtId).ToList();
                ddlUpazila.DataTextField = "Name";
                ddlUpazila.DataValueField = "Id";
                ddlUpazila.DataBind();
                upnlUpazila.Update();
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            List<TempMonthScheduler> schedules = SessionManager.TEMP_NATIONALPLAN_MONTHLY;

            TempMonthScheduler scheduler = new TempMonthScheduler();
            scheduler.Count = Convert.ToInt32(txtCount.Text.Trim());
            scheduler.MonthId = ddlMonth.SelectedItem.Value;
            scheduler.Month = ddlMonth.SelectedItem.Text;
            scheduler.GroupId = ddlChemicalGroup.SelectedItem.Value;
            scheduler.Group = ddlChemicalGroup.SelectedItem.Text;
            scheduler.DivisionId = ddlUpazila.SelectedItem.Value;
            scheduler.Division = ddlUpazila.SelectedItem.Text;
            schedules.Add(scheduler);

            SessionManager.TEMP_NATIONALPLAN_MONTHLY = schedules;
            BindDataGrid(schedules);
            upnlUpazila.Update();
        }

        private void BindDataGrid(List<TempMonthScheduler> schedules)
        {
            repSchedules.DataSource = schedules;
            repSchedules.DataBind();
            var categories =from p in schedules
                            group p by p.Group into g
                            select new { Category = g.Key, TotalUnitsInStock = g.Sum(p => p.Count) };
            List<TempValueObject> planAdded = categories.ToList().ConvertAll(x => new TempValueObject { Key = x.Category, Value = x.TotalUnitsInStock.ToString() });
            List<TempValueObject> plansummary = SessionManager.TEMP_PLAN_SUMMARY;
            var sequenceDiff = plansummary.Except(planAdded);
            if (sequenceDiff.Count() == 0)
            {
                btnSaveAll.Visible = true;
            }
        }

        protected void btnSaveAll_Click(object sender, EventArgs e)
        {

        }

    }
}